
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>测试 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="调试.html" />
    
    
    <link rel="prev" href="标准库.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础.html">
            
                <a href="基础.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="字符串处理.html">
            
                <a href="字符串处理.html">
            
                    
                    字符串处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="数据结构.html">
            
                <a href="数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="数据结构高级内容.html">
            
                <a href="数据结构高级内容.html">
            
                    
                    数据结构高级内容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="函数式编程.html">
            
                <a href="函数式编程.html">
            
                    
                    函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="面向对象编程.html">
            
                <a href="面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="类的特殊方法.html">
            
                <a href="类的特殊方法.html">
            
                    
                    类的特殊方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="IO编程.html">
            
                <a href="IO编程.html">
            
                    
                    IO编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="模块.html">
            
                <a href="模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="标准库.html">
            
                <a href="标准库.html">
            
                    
                    标准库
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.14" data-path="测试.html">
            
                <a href="测试.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="调试.html">
            
                <a href="调试.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >测试</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6D4B;&#x8BD5;">&#x6D4B;&#x8BD5;</h1>
<hr>
<h2 id="&#x5355;&#x5143;&#x6D4B;&#x8BD5;">&#x5355;&#x5143;&#x6D4B;&#x8BD5;</h2>
<blockquote>
<p>Python &#x5185;&#x7F6E;&#x7684; unittest &#x6A21;&#x5757;&#x7528;&#x4E8E;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x3002;  </p>
</blockquote>
<p><strong>&#x7F16;&#x5199;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#xFF1A;</strong><br>&#x7F16;&#x5199;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7F16;&#x5199;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x7C7B;&#xFF0C;&#x5E76;&#x4ECE; <code>unittest.TestCase</code> &#x7EE7;&#x627F;&#x3002;&#x5728;&#x6D4B;&#x8BD5;&#x7C7B;&#x4E2D;&#x53EF;&#x4EE5;&#x7F16;&#x5199;&#x591A;&#x4E2A;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#xFF08;&#x4E5F;&#x53EB;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#xFF09;&#x3002;<br>&#x4EE5; test &#x5F00;&#x5934;&#x7684;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x4EE5; test &#x5F00;&#x5934;&#x7684;&#x65B9;&#x6CD5;&#x4E0D;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#xFF0C;&#x6D4B;&#x8BD5;&#x7684;&#x65F6;&#x5019;&#x4E0D;&#x4F1A;&#x88AB;&#x6267;&#x884C;&#x3002;<br><code>unittest.TestCase</code>&#x63D0;&#x4F9B;&#x4E86;&#x5F88;&#x591A;&#x5185;&#x7F6E;&#x7684;&#x6761;&#x4EF6;&#x5224;&#x65AD;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x8C03;&#x7528;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x5C31;&#x53EF;&#x4EE5;&#x65AD;&#x8A00;&#x8F93;&#x51FA;&#x662F;&#x5426;&#x662F;&#x6211;&#x4EEC;&#x6240;&#x671F;&#x671B;&#x7684;&#x3002;<br>&#x6700;&#x5E38;&#x7528;&#x7684;&#x65AD;&#x8A00;&#x5C31;&#x662F; <code>assertEqual()</code>&#xFF1A;  </p>
<pre><code class="lang-python">self.assertEqual(abs(<span class="hljs-number">-1</span>), <span class="hljs-number">1</span>) <span class="hljs-comment"># &#x65AD;&#x8A00;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x7684;&#x7ED3;&#x679C;&#x4E0E; 1 &#x76F8;&#x7B49;</span>
</code></pre>
<p>&#x53E6;&#x4E00;&#x79CD;&#x91CD;&#x8981;&#x7684;&#x65AD;&#x8A00;&#x5C31;&#x662F;&#x671F;&#x5F85;&#x629B;&#x51FA;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x7684; Error&#xFF0C;&#x4F8B;&#x5982;&#x901A;&#x8FC7; <code>d[&apos;empty&apos;]</code> &#x8BBF;&#x95EE;&#x4E0D;&#x5B58;&#x5728;&#x7684; key &#x65F6;&#xFF0C;&#x65AD;&#x8A00;&#x4F1A;&#x629B;&#x51FA; KeyError&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">with</span> self.assertRaises(KeyError):
    value = d[<span class="hljs-string">&apos;empty&apos;</span>]
</code></pre>
<p><strong> &#x8FD0;&#x884C;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#xFF1A; </strong><br>&#x6700;&#x7B80;&#x5355;&#x7684;&#x8FD0;&#x884C;&#x65B9;&#x5F0F;&#x662F;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x5199;<code>unittest.main()</code>&#x3002;  </p>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    unittest.main()
</code></pre>
<p>&#x53E6;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x662F;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x901A;&#x8FC7;&#x53C2;&#x6570; <code>-m unittest</code> &#x76F4;&#x63A5;&#x8FD0;&#x884C;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code>$ python3 -m unittest testfile1 testfile2 testfile3
</code></pre><p>&#x8FD9;&#x662F;&#x63A8;&#x8350;&#x7684;&#x505A;&#x6CD5;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x4E00;&#x6B21;&#x6279;&#x91CF;&#x8FD0;&#x884C;&#x5F88;&#x591A;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#xFF0C;&#x5E76;&#x4E14;&#xFF0C;&#x6709;&#x5F88;&#x591A;&#x5DE5;&#x5177;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x6765;&#x8FD0;&#x884C;&#x8FD9;&#x4E9B;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x3002;  </p>
<p><strong>setUp &#x4E0E; tearDown&#xFF1A;</strong><br>&#x53EF;&#x4EE5;&#x5728;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x4E2D;&#x7F16;&#x5199;&#x4E24;&#x4E2A;&#x7279;&#x6B8A;&#x7684; <code>setUp()</code>&#x548C; <code>tearDown()</code>&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x4F1A;&#x5206;&#x522B;&#x5728;&#x6BCF;&#x8C03;&#x7528;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x7684;&#x524D;&#x540E;&#x5206;&#x522B;&#x88AB;&#x6267;&#x884C;&#x3002;<br><code>setUp()</code>&#x65B9;&#x6CD5;&#x4E2D;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#x524D;&#x7684;&#x521D;&#x59CB;&#x5316;&#x5DE5;&#x4F5C;&#xFF0C; <code>tearDown()</code>&#x65B9;&#x6CD5;&#x4E2D;&#x6267;&#x884C;&#x6D4B;&#x8BD5;&#x540E;&#x7684;&#x6E05;&#x9664;&#x5DE5;&#x4F5C;&#x3002;</p>
<p>&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> unittest
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">My</span>:</span>   <span class="hljs-comment">#&#x5F85;&#x6D4B;&#x8BD5;&#x7C7B;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sum</span><span class="hljs-params">(self, x, y)</span>:</span>
        <span class="hljs-keyword">return</span> x + y
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sub</span><span class="hljs-params">(self, x, y)</span>:</span>
        <span class="hljs-keyword">return</span> x - y
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestMy</span><span class="hljs-params">(unittest.TestCase)</span>:</span>  <span class="hljs-comment">#&#x6D4B;&#x8BD5;&#x7C7B;</span>
    <span class="hljs-comment">#&#x521D;&#x59CB;&#x5316;&#x5DE5;&#x4F5C;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        self.my = My()
    <span class="hljs-comment">#&#x9000;&#x51FA;&#x6E05;&#x7406;&#x5DE5;&#x4F5C;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">tearDown</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">pass</span>
    <span class="hljs-comment">#&#x5177;&#x4F53;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#xFF0C;&#x4E00;&#x5B9A;&#x8981;&#x4EE5;test&#x5F00;&#x5934;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_1</span><span class="hljs-params">(self)</span>:</span>
        self.assertEqual(self.my.sum(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>), <span class="hljs-number">3</span>)
    <span class="hljs-comment">#&#x5177;&#x4F53;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#xFF0C;&#x4E00;&#x5B9A;&#x8981;&#x4EE5;test&#x5F00;&#x5934;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_2</span><span class="hljs-params">(self)</span>:</span>
        self.assertEqual(self.my.sub(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>), <span class="hljs-number">-1</span>)
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
   unittest.main()   <span class="hljs-comment">#&#x542F;&#x52A8;&#x6D4B;&#x8BD5;</span>
</code></pre>
<p>&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;&#xFF1A;&#x6253;&#x5370;&#x4E00;&#x4E2A;&#x70B9;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x901A;&#x8FC7;&#xFF0C;F&#x8868;&#x793A;&#x4E00;&#x4E2A;fail.</p>
<h2 id="&#x6587;&#x6863;&#x6D4B;&#x8BD5;">&#x6587;&#x6863;&#x6D4B;&#x8BD5;</h2>
<p>Python &#x5185;&#x7F6E;&#x7684;&#x201C;&#x6587;&#x6863;&#x6D4B;&#x8BD5;&#x201D;&#xFF08; doctest&#xFF09;&#x6A21;&#x5757;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x63D0;&#x53D6;&#x6CE8;&#x91CA;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x5E76;&#x6267;&#x884C;&#x6D4B;&#x8BD5;&#x3002;<br>doctest &#x4E25;&#x683C;&#x6309;&#x7167; Python &#x4EA4;&#x4E92;&#x5F0F;&#x547D;&#x4EE4;&#x884C;&#x7684;&#x8F93;&#x5165;&#x548C;&#x8F93;&#x51FA;&#x6765;&#x5224;&#x65AD;&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;&#x662F;&#x5426;&#x6B63;&#x786E;&#x3002; </p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> doctest
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">My</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos;
    Example:
    &gt;&gt;&gt; a = [1,2,3]
    &gt;&gt;&gt; a
    [1, 2, 3]
    &apos;&apos;&apos;</span>
    <span class="hljs-keyword">pass</span>
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    doctest.testmod()
</code></pre>
<p>&#x5982;&#x679C;&#x671F;&#x671B;&#x7ED3;&#x679C;&#x548C;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E0D;&#x4E00;&#x81F4;&#xFF0C;&#x5219;&#x4F1A;&#x62A5;&#x9519;&#xFF1B;&#x5982;&#x679C;&#x6B63;&#x786E;&#xFF0C;&#x5219;&#x4EC0;&#x4E48;&#x8F93;&#x51FA;&#x4E5F;&#x6CA1;&#x6709;&#x3002;  </p>
<blockquote>
<p>&#x6CE8;&#xFF1A;doctest &#x975E;&#x5E38;&#x6709;&#x7528;&#xFF0C;&#x4E0D;&#x4F46;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x6D4B;&#x8BD5;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F5C;&#x4E3A;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x3002;&#x901A;&#x8FC7;&#x67D0;&#x4E9B;&#x6587;&#x6863;&#x751F;&#x6210;&#x5DE5;&#x5177;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x628A;&#x5305;&#x542B; doctest &#x7684;&#x6CE8;&#x91CA;&#x63D0;&#x53D6;&#x51FA;&#x6765;&#x3002;&#x7528;&#x6237;&#x770B;&#x6587;&#x6863;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x770B;&#x5230;&#x4E86; doctest&#x3002;  </p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="标准库.html" class="navigation navigation-prev " aria-label="Previous page: 标准库">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="调试.html" class="navigation navigation-next " aria-label="Next page: 调试">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"测试","level":"1.14","depth":1,"next":{"title":"调试","level":"1.15","depth":1,"path":"调试.md","ref":"调试.md","articles":[]},"previous":{"title":"标准库","level":"1.13","depth":1,"path":"标准库.md","ref":"标准库.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"测试.md","mtime":"2017-08-27T03:38:39.530Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-08-27T04:11:18.522Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

